package cn.zyq.ykb.service.task.impl;


import cn.zyq.ykb.beans.sys.SysAdmin;
import cn.zyq.ykb.beans.task.dto.MyTaskAuditDto;
import cn.zyq.ykb.beans.user.User;
import cn.zyq.ykb.mapper.sys.SysAdminMapper;
import cn.zyq.ykb.mapper.user.UserMapper;
import cn.zyq.ykb.system.common.BaseService;
import com.github.pagehelper.PageHelper;
import cn.zyq.ykb.system.ry.util.SecurityUtils;
import cn.zyq.ykb.mapper.task.MyTaskAuditMapper;
import cn.zyq.ykb.beans.task.MyTaskAudit;
import cn.zyq.ykb.service.task.MyTaskAuditService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.zyq.ykb.system.common.PageParam;
import com.github.pagehelper.PageInfo;


import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * 任务-审核Service业务层处理
 *
 * @author SystemAuth
 * @date 2023-05-24
 */
@Service
@Slf4j
public class MyTaskAuditServiceImpl  extends BaseService implements MyTaskAuditService {

    @Autowired
    private MyTaskAuditMapper myTaskAuditMapper;
    @Autowired
    private SysAdminMapper sysAdminMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 查询任务-审核
     *
     * @param myTaskAuditID 任务-审核主键
     * @return 任务-审核
     */
    @Override
    public MyTaskAudit selectMyTaskAuditByMyTaskAuditID(Integer myTaskAuditID) {
        MyTaskAudit myTaskAudit = myTaskAuditMapper.selectByPrimaryKey(myTaskAuditID);
        if (myTaskAudit.getDeleted() == 1) {
            return null;
        }
        return myTaskAudit;
    }

    /**
     * 查询任务-审核
     *
     * @param serviceID 业务ID
     * @return 任务-审核
     */
    @Override
    public MyTaskAudit selectMyTaskAuditByServiceID(String serviceID) {
        MyTaskAudit param = new MyTaskAudit();
        param.setDeleted(0);
        param.setServiceID(serviceID);
        return this.myTaskAuditMapper.selectOne(param);
    }

    /**
     * 查询任务-审核列表
     *
     * @param pageParam
     * @return 任务-审核
     */
    @Override
    public PageInfo<MyTaskAudit> selectMyTaskAuditList(PageParam pageParam) {
        PageHelper.startPage(pageParam.getPage(), pageParam.getPageSize());
        pageParam.getKv().put("deleted", 0);
        List<MyTaskAudit> list = this.myTaskAuditMapper.list(pageParam.getKv());
        return new PageInfo<>(list);
    }

    /**
     * 查询任务-审核列表
     *
     * @param param
     * @return 任务-审核
     */
    @Override
    public List<MyTaskAudit> selectMyTaskAuditList(MyTaskAudit param) {
        param.setDeleted(0);
        return this.myTaskAuditMapper.select(param);
    }
    /**
     * 新增任务-审核
     *
     * @param param 任务-审核
     * @return 结果
     */
    @Override
    public int insertMyTaskAudit(MyTaskAudit param) {
        this.setCreateBaseData(param, SecurityUtils.getLoginUser().getSysAdmin());
        return this.myTaskAuditMapper.insertSelective(param);
    }

    /**
     * 修改任务-审核
     *
     * @param param 任务-审核
     * @return 结果
     */
    @Override
    public int updateMyTaskAudit(MyTaskAudit param) {
        this.setUpdateBaseData(param, SecurityUtils.getLoginUser().getSysAdmin());
        return this.myTaskAuditMapper.updateByPrimaryKeySelective(param);
    }

    /**
     * 批量删除任务-审核
     *
     * @param myTaskAuditIDs 需要删除的任务-审核主键
     * @return 结果
     */
    @Override
    public int deleteMyTaskAuditByMyTaskAuditIDs(Integer[] myTaskAuditIDs) {
        int i = 0;
        for (Integer myTaskAuditID : myTaskAuditIDs) {
            i += this.deleteMyTaskAuditByMyTaskAuditID(myTaskAuditID);
        }
        return i;
    }

    /**
     * 删除任务-审核信息
     *
     * @param myTaskAuditID 任务-审核主键
     * @return 结果
     */
    @Override
    public int deleteMyTaskAuditByMyTaskAuditID(Integer myTaskAuditID) {
        MyTaskAudit myTaskAudit = this.selectMyTaskAuditByMyTaskAuditID(myTaskAuditID);
        if (myTaskAudit == null) {
            return 0;
        } else {
            MyTaskAudit storeInfo = new MyTaskAudit();
            storeInfo.setMyTaskAuditID(myTaskAuditID);
            storeInfo.setDeleted(1);
            storeInfo.setUpdateBy(SecurityUtils.getSysAdminID());
            storeInfo.setUpdateTime(LocalDateTime.now());
            this.myTaskAuditMapper.updateByPrimaryKeySelective(storeInfo);
            return 1;
        }
    }

    @Override
    public List<MyTaskAuditDto> daoChu(PageParam pageParam, SysAdmin admin) {
        log.info("入参：{}", pageParam);
        PageHelper.startPage(pageParam.getPage(), pageParam.getPageSize());
        pageParam.getKv().put("deleted", 0);
        SysAdmin sysAdmin = this.sysAdminMapper.selectByPrimaryKey(admin.getSysAdminID());
        List<MyTaskAuditDto> myTaskAudits = null;
        if (sysAdmin.getUserType().equals("01")) {
            User user = new User();
            user.setDeleted(0);
            user.setPhone(admin.getPhonenumber());
            User param = this.userMapper.selectOne(user);
            pageParam.getKv().put("userID", param.getUserID());
            PageHelper.startPage(pageParam.getPage(), pageParam.getPageSize());
            myTaskAudits = this.myTaskAuditMapper.daoChu1(pageParam.getKv());
        }else {
            myTaskAudits =this.myTaskAuditMapper.daoChu(pageParam.getKv());
        }
        for (MyTaskAuditDto audit : myTaskAudits) {
//            List<String> strings = JSONArray.parseArray(audit.getInformation(), String.class);
            List<String> strings= Arrays.asList(audit.getInformation().split(","));
            int size = strings.size();
            if (size == 1){
                String nickName = strings.get(0);
                audit.setNickName(nickName);
            }else if (size == 2){
                String nickName = strings.get(0);
                audit.setNickName(nickName);
                String phone = strings.get(1);
                audit.setPhone(phone);
            }else if (size == 3){
                String nickName = strings.get(0);
                audit.setNickName(nickName);
                String phone = strings.get(1);
                audit.setPhone(phone);
                String aliAccount = strings.get(2);
                audit.setAliAccount(aliAccount);
            }
        }

        return myTaskAudits;
    }
}
